home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 834 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.9 KB

  1. From: jgealow@mtl.mit.edu (Jeffrey C. Gealow)
  2. Message-ID: <4j1tm6$bmu@senator-bedfellow.MIT.EDU>
  3. X-Original-Date: 23 Mar 1996 22:24:06 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 24 Mar 96 04:40:44 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: initialization of nonlocal static objects
  9. Organization: MIT Microsystems Technology Laboratories
  10. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  11.     iQBFAgUBMVTSXeEDnX0m9pzZAQEakgF9Gk3zeheK3b8vug16Av9z+hak3HJaev22
  12.     hWIPYUXLd5fZMxE5uk+5AIv0m1NJH5v4
  13.     =edOA
  14.  
  15. I've encountered trouble with the initialization of nonlocal static
  16. objects.  The following parts of the ARM are relevant:
  17.  
  18.   ARM 3.4
  19.   The initialization of nonlocal static objects in a translation unit is
  20.   done before the first use of any function or object defined in that
  21.   translation unit.  Such initializations may be done before the first
  22.   statement of main() or deferred to any point in time before the first
  23.   use of a function or object defined in that translation unit.  The
  24.   default initialization of all static objects to zero is performed
  25.   before any dynamic (that is, run-time) initialization.  No further
  26.   order is imposed on the initialization of objects from different
  27.   translation units.
  28.   
  29.   ARM 3.4 annotation
  30.   A useful technique for insuring that a global object is initialized
  31.   only once and before its first use is to maintain a count of the
  32.   number of translation units using it.
  33.   
  34.   ARM 7.1.6
  35.   A const object may be initialized, but its value may not be changed 
  36.   thereafter.
  37.  
  38. Are the following observations correct?
  39.  
  40.   The technique described in the ARM 3.4 annotation cannot be applied to
  41.   const objects since the values of the objects may not be changed.
  42.  
  43.   The ARM 3.4 guarantee that the "initialization of nonlocal static
  44.   objects in a translation unit is done before the first use of any
  45.   function or object defined in that translation unit" is not accurate.
  46.   Functions or objects defined in a translation unit may be used in the
  47.   initialization of nonlocal static objects defined in other translation
  48.   units.  Therefore, mutual dependencies may make it impossible to honor 
  49.   the guarantee.
  50.  
  51.   The C++ standard could be revised to include a guarantee that whenever
  52.   possible, the initialization of nonlocal static objects in a translation 
  53.   unit is done before any function or object in that translation unit is 
  54.   used to initialize objects in other translation units.  Otherwise, 
  55.   const objects should not be used.
  56.  
  57. The Sun SPARCompiler C++ 4.1 and G++ 2.7.2 don't even honor the ARM
  58. guarantee when there are no mutual dependencies.  The following
  59. example illustrates the problem.  There are four files: library.cc,
  60. library.h, luser.cc, and main.cc.  The behavior of the program depends
  61. on the order in which files are specified in the compile command:
  62.  
  63. gator jgealow % $CC main.cc luser.cc library.cc
  64. gator jgealow % ./a.out
  65. global: 0
  66. local: 6
  67.  
  68. gator jgealow % $CC main.cc library.cc luser.cc
  69. gator jgealow % ./a.out
  70. global: 6
  71. local: 6
  72.  
  73. Jeff
  74.  
  75.  
  76. ::::::::::::::
  77. library.h
  78. ::::::::::::::
  79. class X {
  80. public:
  81.   int i;
  82.   X(int);
  83. };
  84.  
  85. class Y {
  86. public:
  87.   int i;
  88.   Y();
  89. };
  90. ::::::::::::::
  91. library.cc
  92. ::::::::::::::
  93. #include "library.h"
  94.  
  95. const X Int_(0);
  96. const X Int0(1);
  97. const X Int1(2);
  98. const X Intx(3);
  99.  
  100. X :: X( int x) : i(x)
  101. {}
  102.  
  103. Y :: Y( )
  104. {
  105.   i = Int_.i + Int0.i + Int1.i + Intx.i;
  106. }
  107. ::::::::::::::
  108. luser.cc
  109. ::::::::::::::
  110. #include "library.h"
  111.  
  112. Y global;
  113. ::::::::::::::
  114. main.cc
  115. ::::::::::::::
  116. #include <iostream.h>
  117.  
  118. #include "library.h"
  119.  
  120. extern Y global;
  121.  
  122. int main(int, char* argv[])
  123. {
  124.   Y local;
  125.   cout << "global: " << global.i << endl;
  126.   cout << "local: " << local.i << endl;
  127. }
  128. ---
  129. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  130. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  131. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  132. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  133. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  134.